package cvi.com.reader.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cvi.com.reader.R;
import cvi.com.reader.bean.ReportBean;

/**
 * Created by 梅涛 on 2020/8/4.
 */

public class ReportGridViewAdapter extends BaseAdapter {
    private Context mContext;
    private int lastPosition=-1;//定义一个标记为最后选择的位置
    private List<ReportBean.DataBean> dataBeanList;
    private String[] str = null;

    public ReportGridViewAdapter(Context mContext, List<ReportBean.DataBean> dataBeanList) {
        this.mContext = mContext;
        this.dataBeanList = dataBeanList;
    }

    public void setData(List<ReportBean.DataBean> dataBeanList, int lastPos) {
        this.dataBeanList = dataBeanList;
        this.lastPosition = lastPos;
    }

    public void setSeclection(int position) {
        lastPosition = position;
    }

    public ReportGridViewAdapter(Context mContext) {
        super();
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return dataBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View mView, ViewGroup arg2) {
        ViewHolder holder = null;
        ReportBean.DataBean dataBean = dataBeanList.get(position);
        if (mView == null) {
            mView = LayoutInflater.from(mContext).inflate(R.layout.item_report, null);
            holder = new ViewHolder(mView);
            mView.setTag(holder);
        } else {
            holder = (ViewHolder) mView.getTag();
        }
        if (lastPosition == position) {//最后选择的位置
            holder.reportImage.setBackgroundResource(R.mipmap.ic_selete_report);
        } else {
            holder.reportImage.setBackgroundResource(R.mipmap.ic_unselete_report);
        }
        holder.reportContent.setText(dataBean.getName());
        return mView;
    }

    static class ViewHolder {
        @BindView(R.id.report_image)
        ImageView reportImage;
        @BindView(R.id.report_content)
        TextView reportContent;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
